package com.sky.controller.admin;

import com.sky.dto.ViolationRecordPageQueryDTO;
import com.sky.entity.ViolationRecord;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.ViolationRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController("adminViolationController")
@RequestMapping("/admin/violation")
@Api(tags = "违法记录相关接口")
@Slf4j
public class ViolationRecordController {
    @Autowired
    private ViolationRecordService violationRecordService;
    /**
     * 分页查询
     * @param pageQuery
     * @return
     */
    @RequiresRoles(value={"管理员","系统管理员"},logical = Logical.OR)
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> page(ViolationRecordPageQueryDTO pageQuery) {
        PageResult pageResult = violationRecordService.pageQuery(pageQuery);
        return Result.success(pageResult);
    }

    @PostMapping
    @ApiOperation(value = "新增违法记录")
    public Result save(@RequestBody ViolationRecord violationRecord) {
        violationRecordService.save(violationRecord);
        return Result.success();
    }


    @PostMapping("/uploadIllegal")
    public ResponseEntity<String> receiveIllegal(@RequestBody Map<String, String> payload) {
        String subject = payload.get("subject");
        String illegalType = payload.get("illegalType");
        String licensePlate = payload.get("licensePlate");
        String screenshotPath = payload.get("screenshotPath");
        ViolationRecord violationRecord =new ViolationRecord();
        if (Objects.equals(illegalType, "runred")){
            violationRecord.setViolationType(5);
        }else if (Objects.equals(illegalType, "carrunred")){
            violationRecord.setViolationType(4);
        }else if (Objects.equals(illegalType, "touchline")){
            violationRecord.setViolationType(3);
        }else if (Objects.equals(illegalType, "turnwrong")){
            violationRecord.setViolationType(2);
        }else if (Objects.equals(illegalType, "overspeed")){
            violationRecord.setViolationType(1);
        }
        violationRecord.setImagePath(screenshotPath);
        if (Objects.equals(subject, "car")){
            violationRecord.setType(1);
        }else{
            violationRecord.setType(2);
        }
        violationRecord.setCarId(licensePlate);
        violationRecordService.save(violationRecord);
        // 处理数据...
        System.out.println("Received illegal info: " + payload);

        return ResponseEntity.ok("Received");
    }



    @RequiresRoles(value={"管理员","普通用户","系统管理员"},logical = Logical.OR)
    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID查找违法记录")
    public Result<ViolationRecord> getById(@PathVariable Long id){
        ViolationRecord violationRecord = violationRecordService.getById(id);
        return Result.success(violationRecord);
    }
    //违法记录删除
    @RequiresRoles(value={"管理员","系统管理员"},logical = Logical.OR)
    @DeleteMapping
    @ApiOperation("删除违法记录")
    public Result deleteBatch(@RequestParam List<Long> ids){
        try {
            violationRecordService.deleteBatch(ids);
            return Result.success();
        } catch (Exception e) {
            log.error("删除违法记录失败", e);
            return Result.error("删除失败：" + e.getMessage());
        }
    }
}
